<?php

namespace App\Http\Resources\Train;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;
use JsonSerializable;

class TrainListResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array|Arrayable|JsonSerializable
     */
    public function toArray($request)
    {
        $times            = [
            "1000",
            "1400",
            "1900",
            "2400",
        ];
        $res              = parent::toArray($request);
        $start_time_str   = Str::replace(":", "", $res['start_time']);
        $res['time_slot'] = collect($times)->map(function ($v) use ($start_time_str) {
            return $start_time_str <= $v;
        })->filter()->keys()[0] ?? 0;
        unset($res['from_station_name'], $res['from_station_name_uy'], $res['to_station_name'], $res['to_station_name_uy']);
        return $res;
    }
}
